package com.dqp.cslggroup.SubjectView;

import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.bean.course;
import com.dqp.cslggroup.greendao.courseDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 数据类，加载课程数据
 *
 * @author zf
 */
public class SubjectRepertory {

    public static List<MySubject> loadSubjects() {
        List<MySubject> courses = new ArrayList<>();
        String jc, kcmc, xm, cdmc, zcd, xnm;
        int xqj;
        Random random = new Random();
        List<course> courseList = MyApplication.getDaoInstant().getCourseDao().queryBuilder().where(courseDao.Properties.Who.eq(0)).build().list();
        if (courseList.size() > 0) {
            for (com.dqp.cslggroup.bean.course course : courseList) {
                xqj = course.getXqj();
                xnm = course.getXnm();
                jc = course.getJc();
                kcmc = course.getKcmc();
                xm = course.getXm();
                cdmc = course.getCdmc();
                zcd = course.getZcd();
                String weeksString = course.getWeeklist();
                List<Integer> weekList = str2WeekList(weeksString);
                int[] js;
                int start, step;
                js = StringUtil.SelectNumber(jc);
                start = js[0];
                step = js[1] - start + 1;
                courses.add(new MySubject(xnm, kcmc, cdmc, xm, weekList, start, step, xqj, random.nextInt(20), zcd));
            }
        }
        return courses;
    }

    static List<MySubject> loadCouplesSubjects() {
        List<MySubject> courses = new ArrayList<>();
        String jc, kcmc, xm, cdmc, zcd, xnm;
        int xqj;
        Random random = new Random();
        List<course> courseList = MyApplication.getDaoInstant().getCourseDao().queryBuilder().where(courseDao.Properties.Who.eq(1)).build().list();
        if (courseList.size() > 0) {
            for (com.dqp.cslggroup.bean.course course : courseList) {
                xqj = course.getXqj();
                xnm = course.getXnm();
                jc = course.getJc();
                kcmc = course.getKcmc();
                xm = course.getXm();
                cdmc = course.getCdmc();
                zcd = course.getZcd();
                String weeksString = course.getWeeklist();
                List<Integer> weekList = str2WeekList(weeksString);
                int[] js;
                int start, step;
                js = StringUtil.SelectNumber(jc);
                start = js[0];
                step = js[1] - start + 1;
                courses.add(new MySubject(xnm, kcmc, cdmc, xm, weekList, start, step, xqj, random.nextInt(20), zcd));
            }
        }
        return courses;
    }

    public static List<MySubject> loadSubjects(List<course> courseList) {
        List<MySubject> courses = new ArrayList<>();
        String xqjmc, jc, kcmc, xm, cdmc, zcd, xnm;
        int cd_id, xqj;
        Random random = new Random();
        if (courseList.size() > 0) {
            for (com.dqp.cslggroup.bean.course course : courseList) {
                xqj = course.getXqj();
                xnm = course.getXnm();
                jc = course.getJc();
                kcmc = course.getKcmc();
                xm = course.getXm();
                cdmc = course.getCdmc();
                zcd = course.getZcd();
                String weeksString = course.getWeeklist();
                List<Integer> weekList = str2WeekList(weeksString);
                int[] js;
                int start, step;
                js = StringUtil.SelectNumber(jc);
                start = js[0];
                step = js[1] - start + 1;
                courses.add(new MySubject(xnm, kcmc, cdmc, xm, weekList, start, step, xqj, random.nextInt(20), zcd));
            }
        }
        return courses;
    }

    static List<Integer> getWeekList(String weeksString) {
        List<Integer> weekList = new ArrayList<>();
        if (weeksString == null || weeksString.length() == 0) return weekList;
        if (weeksString.contains(",") && weeksString.contains("-")) {
            String[] arr = weeksString.split(",");
            for (String s : arr) {
                weekList.addAll(getWeekList(s));
            }
            return weekList;
        } else if (weeksString.contains("-")) {
            if (weeksString.contains("单")) {
                int[] arr = StringUtil.SelectNumber(weeksString);
                for (int i = arr[0]; i <= arr[1]; i = i + 2) {
                    weekList.add(i);
                }
            } else if (weeksString.contains("双")) {
                int[] arr = StringUtil.SelectNumber(weeksString);
                for (int i = arr[0]; i <= arr[1]; i = i + 2) {
                    weekList.add(i);
                }
            } else {
                int[] arr = StringUtil.SelectNumber(weeksString);
                for (int i = arr[0]; i <= arr[1]; i++) {
                    weekList.add(i);
                }
            }
        } else {
            int[] arr = StringUtil.SelectNumber(weeksString);
            for (int value : arr) {
                weekList.add(value);
            }
        }
        return weekList;
    }

    static List<Integer> str2WeekList(String weeksString) {
        List<Integer> weekList = new ArrayList<>();
        String[] items = weeksString.split(",");
        for (String s : items) {
            weekList.add(Integer.parseInt(s));
        }
        return weekList;
    }


}
